package org.jeecg.netty.sensor.decode;

import java.util.List;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import org.jeecg.netty.SenSorConfigConsts;

/**
 * @author xiebin
 * @Title: LightDecode
 * @ProjectName yungataway
 * @Description: TODO
 * @date 2018/12/615:30
 */
public class SenSorDecoder extends ByteToMessageDecoder {
    	@Override
    	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
    	{

    		int length = in.readableBytes();
    		if(in.readableBytes() < 2)
    		{
    			return;
    		}
    		in.markReaderIndex();
    		int header = in.readUnsignedByte();
			 in.readUnsignedByte();
			int datalength = in.readUnsignedByte();
    		//读取当前位的值
    		//如果不够包的长度
    		if(length < (datalength+4))
    		{
    			in.resetReaderIndex();
    			return;
    		}
    		//回到起始位
    		in.resetReaderIndex();
    		//从起始位开始获取
    		in.markReaderIndex();

    		byte[] bytes = new byte[in.readableBytes()];
    		in.readBytes(bytes);

    		out.add(Unpooled.copiedBuffer(bytes));
    	}
}
